<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>调试器客户端</title>
    <style>
body {
    background-color: black;
    margin: 0;
    padding: 0;
}
div {
    color: white;
    text-align: left;
    padding: 10px;
}
a:link, a:visited {
    color: lightblue;
    text-decoration: underline;
}
a:hover, a:active {
    color: white;
    text-decoration: underline;
}
pre {
    white-space: pre;
    font-family: 'Courier New', Courier, monospace
}
#login, #exec {
    position: fixed;
    left: 0;
    top: 0;
    height: 32px;
    width: 100%;
    background: #404040;
    padding: 8px;
    z-index: 50;
}
#log {
    position: fixed;
    left: 0;
    top: 32px;
    height: calc(100vh - 32px);
    width: 100%;
    overflow: scroll;
    z-index: 10;
}
button {
    background-color: black;
    color: white;
    border: 1px solid white;
    width: 50px;
    font-size: 16px;
    z-index: 60;
}
#address, #command {
    font-size: 16px;
    background-color: inherit;
    color: white;
    border: 2px gray dotted;
    width: calc(100% - 64px);
    z-index: 60;
}
    </style>
</head>
<body>
<div id="login">
<input id="address" type="text" /><button onclick="connect()">连接</button>
</div>
<div id="exec" style="display: none">
<input id="command" type="text" /><button onclick="exec()">&gt;</button>
</div>
<div id="log">
</div>
<script>
var socket;
function get(id) {
    return document.getElementById(id);
}
function connect() {
    var add = get("address").value;
    logText("尝试连接" + add);
    socket = new WebSocket("ws://" + add);
    socket.addEventListener("open", function(event) {
        logText("成功登陆！");
        get("login").style.display = "none";
        get("exec").style.display = "block";
        setTimeout(ping, 10000);
    });
    socket.addEventListener("message", function(event) {
        try {
            var o = JSON.parse(event.data);
            switch (o.type) {
                case "version":
                logText("版本：" + o.version.join(".") + "\n发布日期：" + o.publishDate);
                break;
                case "println":
                    logText("[" + o.level + "]" + o.text);
                break;
            }
        } catch(e) {
            console.log(e);
        }
    });
    socket.addEventListener("close", function(event) {
        logText("连接已关闭");
        get("login").style.display = "block";
        get("exec").style.display = "none";
        socket = null;
    });
}
function exec() {
    var cmd = get("command").value;
    if (cmd.toLowerCase() == "cls") {
        get("log").innerHTML = "";
        return;
    }
    logText("> " + cmd);
    socket.send(JSON.stringify({
        type : "exec",
        cmd : cmd
    }));
}
function ping() {
    if (!socket) return;
    socket.send(JSON.stringify({
        type : "ping"
    }));
    setTimeout(ping, 10000);
}
function logText(text) {
    console.log(text);
    var container = get("log");
    var node = document.createElement("pre");
    node.innerText = text;
    container.appendChild(node);
    setTimeout(function() {
        container.scrollTop = container.scrollHeight;
    }, 150);
}
</script>
</body>
</html>